package states;

import interfaces.StateController;

import java.util.ArrayList;

import objects.*;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import ect.GeneralPause;
import buttons.CircleToggleButton;
import buttons.ClickButton;

public class Menu extends BasicGameState {
	//WaveSource source1,source2,source3;
	//ArrayList<WaveSource> sourceList;
	//ClickButton button1;
	StateController sC=new StateController();
	public Input input; // key imput
	int sourceAmmount=0;
	GeneralPause gPause;
	
	int runnerAmmount=30;
	ArrayList<Runner> runnerList;
	Runner rLeader;
	
	int delay2=300;
	
	CircleToggleButton gotoBlank,gotoBounce,gotoBrick,gotoPong,gotoClose;
	
	private int id;
	Menu(int stateId){
		gPause=new GeneralPause("Main Menu",id);
		gPause.isPaused=true;
		
		gotoBlank=new CircleToggleButton((int) (SlickGame.width*0.3),(int) (SlickGame.height*0.2),25,"Blank",Color.red,Color.green,Color.red.darker((float) 1.4));
		gotoBrick=new CircleToggleButton((int) (SlickGame.width*0.3),(int) (SlickGame.height*0.4),25,"BrickBreaker",Color.orange,Color.green,Color.orange.darker((float) 1.4));
		gotoBounce=new CircleToggleButton((int) (SlickGame.width*0.3),(int) (SlickGame.height*0.6),25,"Bounce",Color.green,Color.green,Color.green.darker((float) 1.4));
		gotoPong=new CircleToggleButton((int) (SlickGame.width*0.7),(int) (SlickGame.height*0.2),25,"Pong",Color.cyan,Color.green,Color.cyan.darker((float) 1.4));
		gotoClose=new CircleToggleButton((int) (SlickGame.width*0.7),(int) (SlickGame.height*0.8),25,"Exit",new Color(34,0,137),Color.green,Color.cyan.darker((float) 1.4));
		
		rLeader=new Runner(SlickGame.width/2-50+(int)(100*Math.random()),SlickGame.height/2-50+(int)(100*Math.random()),5,8,Color.red.darker(),Color.red);
		runnerList=new ArrayList<Runner>();
		for(int i=0; i<runnerAmmount; i++){
			Color randomColor=new Color((float) (Math.random()%0.7+0.3), (float) (Math.random()%0.7+0.3), (float) (Math.random()%0.7+0.3));
			runnerList.add(new Runner((int)(SlickGame.width*Math.random()),(int)(SlickGame.height*Math.random()),3,7,randomColor.darker(),randomColor));
		}
		
		this.id=stateId;
		sC.setDelay(3*SlickGame.fps);
		//button1=new ClickButton(100,100,70,70,"One");
		//sourceList=new ArrayList<WaveSource>();
		//for(int i=0; i<sourceAmmount; i++){
		//	Color randomColor=new Color((float) (Math.random()%0.7+0.3), (float) (Math.random()%0.7+0.3), (float) (Math.random()%0.7+0.3));
		//	sourceList.add(new WaveSource(SlickGame.width/2-50+(int)(100*Math.random()),SlickGame.height/2-50+(int)(100*Math.random()),3,170,(float)(1.06+(0.06*Math.random())),Color.gray.darker(),randomColor));
		//}//randomColor.darker().addToCopy(Color.black))
}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		gPause.init(gc, sbg);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		//g.setFont(GlobalResources.getFont("FontNormal"));
		g.setBackground(Color.black);
		
		//for(int i=0; i<sourceAmmount; i++){
		//	sourceList.get(i).draw(g);
		//}
		
		for(int i=0; i<runnerAmmount; i++){
			runnerList.get(i).draw(g);
		}
		rLeader.draw(g);
		//button1.draw(g);
		//rcE.draw(g);
		
		g.setColor(Color.gray.darker());
		g.drawString("X: "+Mouse.getX()+":"+SlickGame.width+" Y: "+(SlickGame.height-Mouse.getY())+":"+SlickGame.height+" C: "+Mouse.isButtonDown(0), 10, 30);
		g.setColor(Color.gray.darker());
		g.drawString("Menu",SlickGame.width/2-20,20);
		
		if(gPause.isPaused){
			gPause.draw(g);
			gotoBlank.draw(g);
			gotoBrick.draw(g);
			gotoBounce.draw(g);
			gotoPong.draw(g);
			gotoClose.draw(g);
		}
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		this.input = gc.getInput();
		sC.update(input, delta);
		if(!gPause.isPaused){			
			for(int i=0; i<runnerAmmount; i++){
				runnerList.get(i).update(delta,rLeader);
			}
			rLeader.update(delta);
			
			if(sC.key_1){
				sC.key_1=false;
				for(int i=0; i<runnerAmmount; i++){
					runnerList.get(i).x=(float) (((SlickGame.width-20)*Math.random())+10);
					runnerList.get(i).y=(float) (((SlickGame.height-20)*Math.random())+10);
				}
				rLeader.changeDirection=true;
			}
			
			if(sC.key_2){
				sC.key_2=false;
				if(Runner.rateWaveSpawn>6){
					Runner.rateWaveSpawn-=5;
				}
			}
			
			if(sC.key_3){
				sC.key_3=false;
				if(Runner.rateWaveSpawn<100){
					Runner.rateWaveSpawn+=5;
				}
			}
			
			if(sC.key_4){
				sC.key_4=false;
				Runner.debug=!Runner.debug;
			}
			
			if(sC.key_5){
				sC.key_5=false;
				Runner.followAlways=!Runner.followAlways;
			}
			
			if(sC.key_6 && delay2<=0){
				delay2=300;
				for(int i=0; i<runnerAmmount; i++){
					Color randomColor=new Color((float) (Math.random()%0.8+0.2), (float) (Math.random()%0.8+0.2), (float) (Math.random()%0.8+0.2));
					runnerList.get(i).colorWave=randomColor;
					runnerList.get(i).colorMe=randomColor.darker();
				}
			}else{delay2-=delta;}
			
		}else{ // durring pause.
			gPause.update(sbg, delta);
			gotoBlank.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0), delta);
			gotoBrick.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0), delta);
			gotoBounce.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0), delta);
			gotoPong.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0), delta);
			gotoClose.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0), delta);
			
			if(gotoBlank.clicked){
				gotoBlank.clicked=false;
				sbg.enterState(SlickGame.blank);
			}
			
			if(gotoBrick.clicked){
				gotoBrick.clicked=false;
				sbg.enterState(SlickGame.brick);
			}
			
			if(gotoBounce.clicked){
				gotoBounce.clicked=false;
				sbg.enterState(SlickGame.bounce);
			}
			
			if(gotoPong.clicked){
				gotoPong.clicked=false;
				sbg.enterState(SlickGame.pong);
			}
			
			if(gotoClose.clicked){
				gotoClose.clicked=false;
				gc.exit();
			}
		}
		
		
		if(sC.key_lShift){
			sC.key_lShift=false;
			gPause.pauseState=gPause.playState;
			gPause.togglePause();
		}
		
		
		
		//button1.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
		
	}
	
	public static void destroy(){
		
	}

	public int getID() {
		return this.id;
	}


}
